/* $$Id: ResUtils.java 31 2008-01-09 15:22:49Z qxodream $$
 *
 * RssReaderMidlet --  RSS reader for J2ME(midp)
 * The original codes of this porgram is from
 * RssOwlMIDlet(zhengyun@gmail.com)
 *
 * Copyright (c) 2007-2008 QXO(qxodream@gmail.com)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

package com.ultrapower.tools;

import java.io.InputStream;
import java.util.Hashtable;

import javax.microedition.lcdui.Image;

/**
 * ResUtils = Resources Utility 资源相关的工具类 类说明：将本应用中所有的固定字符串均集中于此,提供一些常用的字符串资源
 *
 * @author QXO(qxodream@gmail.com)
 *
 */
public class ResUtils {

	private static ResUtils instance = new ResUtils();

	private ResUtils() {
		super();
	}

	/*
	 * RecordStore名
	 */
	public static final String RECORDSTORE_NAME = "RSSFEEDS_LIST";

	/*
	 * Feed列表最大的数目
	 */
	public static final int MAX_FEEDS = 15;

	// Image keys
	// private static final int OFFSET_IMG = 100;
	public static final String IMG_ABOUT = "promotion.PNG";
	public static final String IMG_FEEDLIST = "feed.PNG";
	public static final String IMG_FEEDADD = "add_feed.PNG";
	public static final String IMG_UNREADFEED = "feed_unread.PNG";
	public static final String IMG_EDITFEED = "editrssfeed.PNG";
	public static final String IMG_ERROR = "error.PNG";

	/** Image cache */
	protected static Hashtable m_images = new Hashtable();

	/**
	 * Returns specified image.
	 *
	 * @param id
	 *            The id of the image.
	 * @return An image.
	 */
	public static synchronized Image getImage(String imgName) {
		// id -= OFFSET_IMG;
		Image img = (Image) m_images.get(imgName);
		if (img == null) {
			try {
				/*
				 * 注意原来这里写的是"/" +，始终无法加载图像； 所以我改为了"/res/" +
				 */
				final String uri = "/icon/" + imgName;
				/**
				 * SResources.class.getResourcesAsStream(...)方法时采用antenna编译会报如下
				 * [wtkbuild] Error preverifying class
				 * com.ultrapower.tools.SResources [wtkbuild] VERIFIER ERROR
				 * com/ultrapower/tools/SResources.class$(Ljava/lang/String;)Ljava/lang/Class;:
				 * [wtkbuild] Cannot find class java/lang/NoClassDefFoundError
				 * [wtkbuild] Error preferifying, log file not found:
				 *
				 * 而采用实例的getClass().getResourceAsStream(...)则无此问题
				 */
				InputStream imgInput = instance.getClass().getResourceAsStream(
						uri);
				if (null == imgInput) {
					System.out.println("can not find the image:" + uri);
					return null;
				}
				img = Image.createImage(imgInput);
				// img = Image.createImage("/icon/" + IMGNAME_MAP[id]);
				System.out.println(uri);

				// System.out.println("get image:/res/icon/" + IMGNAME_MAP[id]);
				m_images.put(imgName, img);
			} catch (Exception e) {
				System.out.println("Error getting resource img " + imgName
						+ ">>" + e.getMessage());
				e.printStackTrace();
			}
		}
		return img;
	}

	/*
	 * 菜单的名字:
	 */
	// public static String MENU_COMMAND_QUIT = "退出";
	// public static String MENU_COMMAND_READRSSFEED = "读取新闻";
	// public static String MENU_COMMAND_ADDRSSFEED = "添加书签";
	// public static String MENU_COMMAND_EDITRSSFEED = "编辑书签";
	// public static String MENU_COMMAND_DELETERSSFEED = "删除书签";
	// public static String MENU_COMMAND_ABOUTME = "关于我";
	// public static String MENU_COMMAND_VIEWDETAIL = "浏览摘要";
	// public static String MENU_COMMAND_BACKCMD = "返回";
	// public static String MENU_COMMAND_UPDATENEWS = "重新读取新闻";
	// Text keys
	// private static int TXTID = 0;
	// command text
	public static final String CMD_QUIT = "cmdQuit";
	public static final String CMD_SAVE = "cmdSave";

	// public static int TXT_CMD_R_FEED = TXTID++;//R=read
	// public static int TXT_CMD_C_FEED = TXTID++;// C=create
	// public static int TXT_CMD_E_FEED = TXTID++;// E=edit
	public static final String KEY_DEL_FEED = "delFeed";// D=delete
	// public static final String TXT_CMD_ABOUTME = "a b;
	// public static int TXT_CMD_V_DETAIL = TXTID++;// V=View
	public static final String CMD_BACK = "cmdBack";
	public static final String TXT_CMD_UPDATENEWS = "cmdUpdateNews";

	//
	// /*
	// * 按钮的命令文字
	// */
	// protected static final String[] TEXTBUF = new String[TXTID];
	static {
		// TEXTBUF[TXT_COMMAND_QUIT] = "退出";
		// TEXTBUF[TXT_CMD_R_FEED] = "读取新闻";
		// TEXTBUF[TXT_CMD_C_FEED] = "添加书签";
		// TEXTBUF[TXT_CMD_E_FEED] = "编辑书签";
		// TEXTBUF[TXT_CMD_D_FEED] = "删除书签";
		// TEXTBUF[TXT_CMD_ABOUTME] = "关于我";
		// TEXTBUF[TXT_CMD_V_DETAIL] = "浏览摘要";
		// TEXTBUF[TXT_CMD_BACKCMD] = "返回";
		// TEXTBUF[TXT_CMD_UPDATENEWS] = "重新读取新闻";
	}

	/*
	 * 各Form的标识key:
	 */
	public static final String FORM_FEEDLIST = "formFeedList";
	public static String KEY_READ_FEED = "readFeed";
	public static String KEY_ADD_FEED = "addFeed";
	public static String KEY_VIEW_DETAIL = "viewDetail";
	public static String KEY_EDIT_FEED = "editFeed";
	public static String KEY_ABOUTME = "aboutMe";

	public static final String KEY_APPNAME = "appName";

	public static final String RSS_TITLE_KEY = "rssTitle";

	public static final String RSS_LINK_KEY = "rssLink";

	public static final String RSS_CONTENT_KEY = "rssContent";

	public static final String FEED_READING_MSG = "feedReadingMsg";

	public final static String KEY_CR_MSG = "copyrightMsg";

	/**
	 * 此Hashtable存放key-label映射 尽量把i18n相关的资源放入此Hashtable,以便日后替换实现来切换各种语言
	 */
	private static Hashtable keyLabelMap = new Hashtable();

	static {
		keyLabelMap.put(KEY_APPNAME, "RSS无线阅读器");
		keyLabelMap.put(FORM_FEEDLIST, "Rss Feeds");
		keyLabelMap.put(KEY_READ_FEED, "读取新闻");
		keyLabelMap.put(KEY_ADD_FEED, "添加书签");
		keyLabelMap.put(KEY_VIEW_DETAIL, "查看摘要");
		keyLabelMap.put(KEY_EDIT_FEED, "编辑书签");
		keyLabelMap.put(KEY_ABOUTME, "关于我");

		keyLabelMap.put(CMD_QUIT, "退出");
		keyLabelMap.put(CMD_SAVE, "保存");
		keyLabelMap.put(CMD_BACK, "返回");

		keyLabelMap.put(KEY_DEL_FEED, "删除书签");
		keyLabelMap.put(TXT_CMD_UPDATENEWS, "重新读取新闻");
		keyLabelMap.put(KEY_CR_MSG, keyLabelMap.get(KEY_APPNAME) + "\n"
				+ "版本:0.1 发布日期:2008.1.9\n" + "作者:qxodream@gmail.com\n"
				+ "本软件采用GPL2.1协议\n");
		keyLabelMap.put(RSS_TITLE_KEY, "名称");
		keyLabelMap.put(RSS_LINK_KEY, "Rss链接");
		keyLabelMap.put(RSS_CONTENT_KEY, "Rss摘要");
		keyLabelMap.put(FEED_READING_MSG,"正在读取新闻列表，请稍等...");

	}

	public static String getAppName() {
		return getLabel(KEY_APPNAME);
	}

	/**
	 * @param key -
	 *            键值
	 * @return 根据键值来获取对应的Label字串符串;如果没有对应的key,就返回key
	 */
	public static String getLabel(String key) {
		String label = (String) keyLabelMap.get(key);
		return label != null ? label : key;
	}

	/**
	 *
	 * @param key
	 * @return
	 */
	public static String getFormTitle(String key) {
		StringBuffer sb = new StringBuffer(getAppName());
		sb.append(" - ").append(getLabel(key));
		return sb.toString();
	}

	/**
	 * 标识是否显示ItemLabel
	 */
	public static boolean showItemLabel = false;

	/**
	 *
	 * @param key
	 * @return 如果showItemLabel=tru,返回key对应的文字;否则返回"".
	 */
	public static String getItemLabel(String key) {
		return showItemLabel ? getLabel(key) + ":" : "";
	}

	/*
	 * 预定义的Rss Feed名称以及URL:
	 */
	private static String[] feeds = new String[] {
			"solidot|http://solidot.org/index.rss",
			"gseeker|http://www.gseeker.com/index.rss",
			"新浪-国内焦点新闻|http://rss.sina.com.cn/news/china/focus15.xml",
			"新华网-国内新闻|http://rss.xinhuanet.com/rss/native.xml",
			"ChinaUnix.net|http://bbs.chinaunix.net/rss/46.xml",
			"ChinaUnix.net|http://bbs.chinaunix.net/rss/46.xml",
			"DOUBAN MOVIE|http://www.douban.com/feed/review/latest",
			"JOYCODE|http://blog.joycode.com/MainFeed.aspx",
			"CNBlogs|http://www.cnblogs.com/rss.aspx",
			"keso's blog|http://feeds.feedburner.com/PlayinWithIt"

	};

	public static String[] getFeeds() {
		return feeds;
	}

	// /**
	// * Returns specified text as string.
	// *
	// * @param id
	// * The id of the text.
	// * @return A text as String.
	// */
	// public static String getString(int id) {
	// return TEXTBUF[id];
	// }

}
